﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using CuaHangLaptop.BIZ;

namespace CuaHangLaptop.DAL
{
    class HoaDon_DAL
    {
        internal static int Get_Lasted_ID()
        {
            int id = 0;
            SqlConnection conn = Connection.Connection_String();
            SqlDataReader rdr = null;
            try
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand("select id from HoaDon order by id desc", conn);
                rdr = cmd.ExecuteReader();
                rdr.Read();
                id = rdr.GetInt32(0);
            }
            catch
            {
            }
            finally
            {
                if (rdr != null) rdr.Close();
                conn.Close();
            }
            return id;
        }

        // SELECT * FROM NHA SAN XUAT
        internal static List<HoaDon_BIZ> GetAll_HD()
        {
            List<HoaDon_BIZ> result = new List<HoaDon_BIZ>();
            SqlConnection conn = Connection.Connection_String();
            SqlDataReader rdr = null;
            try
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand("select * from HoaDon", conn);
                rdr = cmd.ExecuteReader();

                while (rdr.Read())
                {
                    HoaDon_BIZ dd = new HoaDon_BIZ(rdr.GetInt32(0), rdr.GetString(1), rdr.GetString(2), rdr.GetDateTime(3), rdr.GetDecimal(4), rdr.GetBoolean(5));
                    result.Add(dd);
                }
            }
            finally
            {
                if (rdr != null) rdr.Close();
                conn.Close();
            }
            return result;
        }

        // LẤY NHÀ SẢN XUẤT CÓ MANSX = NSX.MANSX
        internal static HoaDon_BIZ Get_HoaDon_ByCode(string _maHD)
        {
            HoaDon_BIZ hd1 = new HoaDon_BIZ();
            SqlConnection conn = Connection.Connection_String();
            SqlDataReader rdr = null;
            try
            {
                conn.Open();
                String s = String.Format("select * from HoaDon where MaHoaDon='{0}'", _maHD);
                SqlCommand cmd = new SqlCommand(s, conn);
                rdr = cmd.ExecuteReader();
                rdr.Read();
                hd1 = new HoaDon_BIZ(rdr.GetInt32(0), rdr.GetString(1), rdr.GetString(2), rdr.GetDateTime(3), rdr.GetDecimal(4), rdr.GetBoolean(5));
                   
            }
            finally
            {
                if (rdr != null) rdr.Close();
                conn.Close();
            }
            return hd1;
        }

        // LẤY TẤT CẢ NHÀ SẢN XUẤT CÓ MANSX = NSX.MANSX
        internal static List<HoaDon_BIZ> GetAll_HoaDon_ByKH(string _maKH)
        {
            List<HoaDon_BIZ> result = new List<HoaDon_BIZ>();
            SqlConnection conn = Connection.Connection_String();
            SqlDataReader rdr = null;
            try
            {
                conn.Open();
                String s = String.Format("select * from HoaDon where MaKH='{0}'", _maKH);
                SqlCommand cmd = new SqlCommand(s, conn);
                rdr = cmd.ExecuteReader();

                while (rdr.Read())
                {
                    HoaDon_BIZ dd = new HoaDon_BIZ(rdr.GetInt32(0), rdr.GetString(1), rdr.GetString(2), rdr.GetDateTime(3), rdr.GetDecimal(4), rdr.GetBoolean(5));            
                    result.Add(dd);
                }
            }
            finally
            {
                if (rdr != null) rdr.Close();
                conn.Close();
            }
            return result;
        }

        // DELETE
        internal static void Delete(HoaDon_BIZ hd)
        {
            SqlConnection conn = Connection.Connection_String();
            try
            {
                conn.Open();
                String s = String.Format("delete from HoaDon where id={0}", hd.ID);
                SqlCommand cmd = new SqlCommand(s, conn);

                if (cmd.ExecuteNonQuery() > 0)
                {
                }
            }
            finally
            {
                conn.Close();
            }
        }

        // UPDATE
        internal static void Update(HoaDon_BIZ hd)
        {
            SqlConnection conn = Connection.Connection_String();
            string s ="";
            try
            {
                conn.Open();
                s = String.Format("update  HoaDon set Mahoadon ='{0}', MaKH ='{1}', NgayLapHoaDon='{2}', TongTien ={3},TinhTrang='{4}' where id={5}", hd.MaHoaDon, hd.KhachHang.MaKH, hd.NgayLap, hd.TongTien, hd.TinhTrang, hd.ID);
                SqlCommand cmd = new SqlCommand(s, conn);

                if (cmd.ExecuteNonQuery() > 0)
                {
                }
            }
                
            finally
            {
                conn.Close();
            }
        }

        // INSERT
        internal static int Insert(HoaDon_BIZ hd)
        {
            SqlConnection conn = Connection.Connection_String();
            int newId = -1;
            SqlDataReader rdr = null;
            try
            {
                conn.Open();
                String s = String.Format("insert into HoaDon values('{0}', '{1}','{2}',{3},'{4}')", hd.MaHoaDon, hd.KhachHang.MaKH, hd.NgayLap, hd.TongTien, hd.TinhTrang);
                SqlCommand cmd = new SqlCommand(s, conn);

                if (cmd.ExecuteNonQuery() > 0)
                {
                    s = String.Format("select id from HoaDon where MaHoaDon='{0}'", hd.MaHoaDon);
                    cmd.CommandText = s;
                    rdr = cmd.ExecuteReader();

                    if (rdr.HasRows)
                    {
                        rdr.Read();
                        newId = Int32.Parse(rdr["id"].ToString());
                    }
                }
            }
            finally
            {
                if (rdr != null) rdr.Close();
                conn.Close();
            }
            return newId;
        }
    }
}
